package com.ruoyi.business.service.impl;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.dto.TrainCarriageGroupDTO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TrainCarriageMapper;
import com.ruoyi.business.domain.TrainCarriage;
import com.ruoyi.business.service.ITrainCarriageService;

/**
 * 火车车厢Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class TrainCarriageServiceImpl implements ITrainCarriageService
{
    @Autowired
    private TrainCarriageMapper trainCarriageMapper;

    /**
     * 查询火车车厢
     *
     * @param id 火车车厢主键
     * @return 火车车厢
     */
    @Override
    public TrainCarriage selectTrainCarriageById(Long id)
    {
        return trainCarriageMapper.selectTrainCarriageById(id);
    }

    /**
     * 查询火车车厢列表
     *
     * @param trainCarriage 火车车厢
     * @return 火车车厢
     */
    @Override
    public List<TrainCarriage> selectTrainCarriageList(TrainCarriage trainCarriage)
    {
        return trainCarriageMapper.selectTrainCarriageList(trainCarriage);
    }

    /**
     * 新增火车车厢
     *
     * @param trainCarriage 火车车厢
     * @return 结果
     */
    @Override
    public int insertTrainCarriage(TrainCarriage trainCarriage)
    {
        trainCarriage.setCreateTime(DateUtils.getNowDate());
        return trainCarriageMapper.insertTrainCarriage(trainCarriage);
    }

    /**
     * 修改火车车厢
     *
     * @param trainCarriage 火车车厢
     * @return 结果
     */
    @Override
    public int updateTrainCarriage(TrainCarriage trainCarriage)
    {
        trainCarriage.setUpdateTime(DateUtils.getNowDate());
        return trainCarriageMapper.updateTrainCarriage(trainCarriage);
    }

    /**
     * 批量删除火车车厢
     *
     * @param ids 需要删除的火车车厢主键
     * @return 结果
     */
    @Override
    public int deleteTrainCarriageByIds(Long[] ids)
    {
        return trainCarriageMapper.deleteTrainCarriageByIds(ids);
    }

    /**
     * 删除火车车厢信息
     *
     * @param id 火车车厢主键
     * @return 结果
     */
    @Override
    public int deleteTrainCarriageById(Long id)
    {
        return trainCarriageMapper.deleteTrainCarriageById(id);
    }

    /**
     * 批量新增火车车厢
     * @param trainCarriage
     * @return 结果
     */
    @Override
    public int insertTrainCarriages(List<TrainCarriage> trainCarriage) {
        return trainCarriageMapper.insertTrainCarriages(trainCarriage);
    }

    /**
     * 按车次编号分组查询火车车厢
     *
     * @param trainCarriage 查询参数
     * @return 分组后的火车车厢列表
     */
    @Override
    public List<TrainCarriageGroupDTO> selectTrainCarriageGroups(TrainCarriage trainCarriage) {
        List<TrainCarriage> trainCarriages = selectTrainCarriageList(trainCarriage);

        // 按车次编号分组
        Map<String, List<TrainCarriage>> groups = trainCarriages.stream()
                .collect(Collectors.groupingBy(TrainCarriage::getTrainCode));

        // 处理每个分组
        return groups.entrySet().stream().map(entry -> {
            String trainCode = entry.getKey();
            List<TrainCarriage> carriages = entry.getValue();

            // 按厢号排序
            carriages.sort(Comparator.comparing(TrainCarriage::getIndex));

            TrainCarriageGroupDTO groupDTO = new TrainCarriageGroupDTO();
            groupDTO.setTrainCode(trainCode);
            groupDTO.setCarriages(carriages);

            // 计算总座位数
            long totalSeats = carriages.stream()
                    .mapToLong(c -> c.getSeatCount() != null ? c.getSeatCount() : 0)
                    .sum();
            groupDTO.setTotalSeats(totalSeats);

            // 统计各类型座位的车厢数
            Map<String, Integer> seatTypeCounts = new HashMap<>();
            for (TrainCarriage carriage : carriages) {
                String seatType = carriage.getSeatType();
                seatTypeCounts.put(seatType, seatTypeCounts.getOrDefault(seatType, 0) + 1);
            }
            groupDTO.setSeatTypeCounts(seatTypeCounts);

            return groupDTO;
        }).collect(Collectors.toList());
    }

    /**
     * 下载火车车厢导入模板 - 不包含错误字段
     *
     * @param response HTTP响应
     * @throws IOException IO异常
     */
    @Override
    public void downloadCustomTemplate(HttpServletResponse response) throws IOException {
        com.ruoyi.common.utils.poi.ExcelUtil<com.ruoyi.business.domain.vo.TrainCarriageVo> util = 
            new com.ruoyi.common.utils.poi.ExcelUtil<>(com.ruoyi.business.domain.vo.TrainCarriageVo.class);
        util.importTemplateExcel(response, "火车车厢数据");
    }
    
    /**
     * 清空火车车厢数据
     * 
     * @return 结果
     */
    @Override
    public int clearTrainCarriage() {
        return trainCarriageMapper.clearTrainCarriage();
    }
}
